import BaseView from "../base/BaseView";
import BaseApp from "../base/BaseApp";
import TipsView,{TipsType} from "./common/TipView";
import { Button, resources, Sprite, SpriteFrame  } from "cc";

export default class TestView2 extends BaseView {
	get url():string{return "Test:test2"}

	onLoad(){
		const button = this.node.getChildByPath("button1");

		button.on("click", this.onBtnClickEvent, this);
		this.node.getChildByPath("button3").on("click", this.onBtnClickEvent2, this);
		this.node.getChildByPath("buttonall").on("click", this.onBtnClickEvent2, this);
	}

	onBtnClickEvent2(btn:Button):void{
		if(btn.node.name == "buttonall"){
			BaseApp.instance().closeAllView();
		}else{
			const spNode = this.node.getChildByName("sprite");

			resources.load("HelloWorld/spriteFrame", SpriteFrame, (err, spriteFrame) => {
				const sp =  spNode.getComponent(Sprite)

				sp.spriteFrame = spriteFrame;
			});

			let tipsType = TipsType.TC;

			if(Math.random()>0.5) tipsType =TipsType.BC;
			TipsView.createTips("=============dfff"+ Math.floor(Math.random()*5),tipsType);
			BaseApp.instance().eventTarget.emit("foobar","message from testview2 onBtnClickEvent2");
		}
	}

	onBtnClickEvent(event):void{
		if(this.mParent!= null){
			this.closeView();
			BaseApp.instance().eventTarget.emit("foobar","CloseView from testview2");
		}
		else{
			BaseApp.instance().closeViewByName(this.node.name);
		}        
	}
}